//
//  RegisterIntentionViewController.m
//  Btpin
//
//  Created by Vic on 2019/5/31.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "RegisterIntentionViewController.h"
#import "SelcetCityViewController.h"
#import "PMineResumeTradeViewController.h"

#import "LYCareerObjectiveSelectViewController.h"
#import "SelectJobCategoryViewController.h"

@interface RegisterIntentionViewController ()<PSSingleRowViewDelegate, SelcetCityViewControllerDelegate, PMineResumeTradeViewControllerDelegate, SelectJobCategoryViewControllerDelegate>
//PSSalaryRangeViewDelegate
{
    NSString *jobsNature;
    NSString *trade;
    NSString *catgory;
    NSString *wage;
    NSString *city;
}
@property (nonatomic, strong) PSCell *jobsNatureCell;
@property (nonatomic, strong) PSCell *tradeCell;
@property (nonatomic, strong) PSCell *catgoryCell;
@property (nonatomic, strong) PSCell *wageCell;
@property (nonatomic, strong) PSCell *cityCell;
@property (nonatomic, strong) PSSingleRowView *jobsNaturePickerView;
@property (nonatomic, strong) PSSingleRowView *wagePickerView;
//@property (nonatomic, strong) PSSalaryRangeView *wagePickerView;
@property (nonatomic, strong) PSSingleRowView *jobCatsPickerView;
@end

@implementation RegisterIntentionViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.jobsNaturePickerView) {
        self.jobsNaturePickerView.hidden = YES;
    }
    if (self.wagePickerView) {
        self.wagePickerView.hidden = YES;
    }
    if (self.jobCatsPickerView) {
        self.jobCatsPickerView.hidden = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    self.navigationItem.title = @"求职意向";
    
    jobsNature = @"";
    trade = @"";
    catgory = @"";
    wage = @"";
    city = @"";
    
    [self initUI];
}
- (void)initUI {
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(WIDTH-50*Scale, HeightOfStatusBar+3*Scale, 50*Scale, 36*Scale) imageName:nil target:self action:@selector(customBackButtonTap) title:@"确定" titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:okBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    __weak __typeof(self)weakSelf = self;
    self.jobsNaturePickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.jobsNatureArray target:self];
    self.jobsNaturePickerView.hidden = YES;
    self.wagePickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.wageArray target:self];
    self.wagePickerView.hidden = YES;
    self.jobCatsPickerView = [[PSSingleRowView alloc] initWithData:[DataToolsControl sharedManager].allCategoryInfoModel.jobCatsArray target:self];
    self.jobCatsPickerView.hidden = YES;
    
    self.jobsNatureCell = [[PSCell alloc] initWithFrame:CGRectMake(30*Scale, 15*Scale, WIDTH-60*Scale, PSCellHeight) title:@"工作性质" text:((self.infoDataModel && self.infoDataModel.nature_cn.length)?self.infoDataModel.nature_cn:nil) placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    self.jobsNatureCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.jobsNaturePickerView.hidden = NO;
//        [strongSelf jumpToSelectPosition];
    };
    [self.view addSubview:self.jobsNatureCell];
    if (self.infoDataModel && self.infoDataModel.nature.length) {
        jobsNature = self.infoDataModel.nature;
    }
    
    NSString *trade1 = @"";
    if (self.infoDataModel && self.infoDataModel.trade_cn.length) {
        trade1 = self.infoDataModel.trade_cn;
        trade = self.infoDataModel.trade;
    }
    self.tradeCell = [[PSCell alloc] initWithFrame:CGRectMake(StartX(self.jobsNatureCell), EndY(self.jobsNatureCell), ViewWidth(self.jobsNatureCell), PSCellHeight) title:@"期望行业" text:trade1 placeholder:@"最多选三个" cellType:PSCellTypeOfIndicator];
    self.tradeCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        PMineResumeTradeViewController *PMRTVC = [[PMineResumeTradeViewController alloc] init];
        PMRTVC.delegate = strongSelf;
        [strongSelf.navigationController pushViewController:PMRTVC animated:YES];
    };
    [self.view addSubview:self.tradeCell];
    
    NSString *catgory1 = @"";
    if (self.infoDataModel && self.infoDataModel.jobs.length) {
        catgory1 = self.infoDataModel.jobs;
        catgory = self.infoDataModel.jobs_id;
    }
    self.catgoryCell = [[PSCell alloc] initWithFrame:CGRectMake(StartX(self.jobsNatureCell), EndY(self.tradeCell), ViewWidth(self.jobsNatureCell), PSCellHeight) title:@"期望职位" text:catgory1 placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    self.catgoryCell.tap = ^{
//        __strong __typeof(weakSelf)strongSelf = weakSelf;
//        strongSelf.jobCatsPickerView.hidden = NO;
        [weakSelf jumpToSelectPosition];
    };
    [self.view addSubview:self.catgoryCell];
    
    if (self.infoDataModel && self.infoDataModel.wage.length) {
        wage = self.infoDataModel.wage;
    }
    self.wageCell = [[PSCell alloc] initWithFrame:CGRectMake(StartX(self.jobsNatureCell), EndY(self.catgoryCell), ViewWidth(self.jobsNatureCell), PSCellHeight) title:@"期望薪资" text:((self.infoDataModel && self.infoDataModel.wage_cn.length)?self.infoDataModel.wage_cn:@"") placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    self.wageCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.wagePickerView.hidden = NO;
    };
    [self.view addSubview:self.wageCell];
    
    NSString *city1 = @"";
    if (self.infoDataModel && self.infoDataModel.district_cn.length) {
        city1 = self.infoDataModel.district_cn;
        city = self.infoDataModel.district;
    }
    self.cityCell = [[PSCell alloc] initWithFrame:CGRectMake(StartX(self.wageCell), EndY(self.wageCell), ViewWidth(self.wageCell), PSCellHeight) title:@"工作城市" text:city1 placeholder:@"请选择" cellType:PSCellTypeOfIndicator];
    self.cityCell.tap = ^{
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        SelcetCityViewController *SCVC = [[SelcetCityViewController alloc] init];
        SCVC.delegate = strongSelf;
        [strongSelf.navigationController pushViewController:SCVC animated:YES];
    };
    [self.view addSubview:self.cityCell];
}
- (void)customBackButtonTap {
    if (self.jobsNatureCell.textField.text.length) {
        if (self.tradeCell.textField.text.length) {
            if (self.catgoryCell.textField.text.length) {
                if (self.wageCell.textField.text.length) {
                    if (self.cityCell.textField.text.length) {
                        [self upDate];
                    } else {
                        SVPHUDError(@"请选择期望城市");
                    }
                } else {
                    SVPHUDError(@"请选择期望薪资");
                }
            } else {
                SVPHUDError(@"请选择期望职位");
            }
        } else {
            SVPHUDError(@"请选择期望行业");
        }
    } else {
        SVPHUDError(@"请选择工作性质");
    }
}
- (void)upDate {
//    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:wage forKey:@"wage"];
    [dic setObject:jobsNature forKey:@"job_nature"];
    [dic setObject:catgory forKey:@"job_cats"];
    [dic setObject:city forKey:@"district"];
    [dic setObject:trade forKey:@"trade"];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(registerIntentionViewControllerBackData:)]) {
        [self.delegate registerIntentionViewControllerBackData:self.infoDataModel];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)psSingleRowViewCancel:(PSSingleRowView *)psSingleRowView {
    psSingleRowView.hidden = YES;
}
- (void)psSingleRowViewOk:(PublicCategoryElementModel *)model PSSingleRowView:(PSSingleRowView *)psSingleRowView {
    psSingleRowView.hidden = YES;
    if (psSingleRowView == self.jobsNaturePickerView) {
        self.jobsNatureCell.textField.text = model.name;
        jobsNature = model.id;
        self.infoDataModel.nature = model.id;
        self.infoDataModel.nature_cn = model.name;
    } else if (psSingleRowView == self.wagePickerView) {
        self.wageCell.textField.text = model.name;
        wage = model.id;
        self.infoDataModel.wage = model.id;
        self.infoDataModel.wage_cn = model.name;
    } else if (psSingleRowView == self.jobCatsPickerView) {
        self.catgoryCell.textField.text = model.name;
        catgory = model.id;
        self.infoDataModel.catgory_id = model.id;
        self.infoDataModel.catgory_cn = model.name;
    }
}
- (void)selcetCityViewControllerInfo:(NSDictionary *)dic {
    if ([[NSString stringWithFormat:@"%@", dic[@"cityid"]] integerValue] == 0) {
        self.cityCell.textField.text = [NSString stringWithFormat:@"%@", dic[@"province"]];
        city = [NSString stringWithFormat:@"%@", dic[@"provinceid"]];
        
        self.infoDataModel.district_cn = [NSString stringWithFormat:@"%@", dic[@"province"]];
        self.infoDataModel.district = [NSString stringWithFormat:@"%@", dic[@"provinceid"]];
    } else {
        self.cityCell.textField.text = [NSString stringWithFormat:@"%@-%@", dic[@"province"], dic[@"city"]];
        city = [NSString stringWithFormat:@"%@", dic[@"cityid"]];
        
        self.infoDataModel.district_cn = [NSString stringWithFormat:@"%@-%@", dic[@"province"], dic[@"city"]];
        self.infoDataModel.district = [NSString stringWithFormat:@"%@", dic[@"cityid"]];
    }
}
- (void)PMineResumeTradeViewControllerBackInfo:(NSArray *)info {
    NSLog(@"%@", info);
    NSString *name = @"";
    trade = @"";
    for (PublicCategoryElementModel *model in info) {
        trade = [NSString stringWithFormat:@"%@,%@", model.id, trade];
        name = [NSString stringWithFormat:@"%@,%@", model.name, name];
    }
    if (trade && trade.length>1) {
        trade = [trade substringToIndex:trade.length-1];
    }
    if (name && name.length>1) {
        name = [name substringToIndex:name.length-1];
    }
    self.tradeCell.textField.text = name;
    
    self.infoDataModel.trade = trade;
    self.infoDataModel.trade_cn = name;
}

#pragma mark-- Action
- (void)jumpToSelectPosition {
    SelectJobCategoryViewController *SJCVC = [[SelectJobCategoryViewController alloc] init];
    SJCVC.navigationItem.title = @"期望职位";
    SJCVC.delegate = self;
    [self.navigationController pushViewController:SJCVC animated:YES];
}
- (void)selectJobCategoryViewControllerInfo:(SelectJobCategoryModel *)model {
    self.catgoryCell.textField.text = model.threeLevelName;
    catgory = model.threeLevelID;
    
    self.infoDataModel.jobs = model.threeLevelName;
    self.infoDataModel.jobs_id = model.threeLevelID;
}

@end
